package com.mingtu.guards.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import com.mingtu.center.activity.Ranking2Activity;
import com.mingtu.center.activity.RecordActivity;
import com.mingtu.center.activity.TrackGradeActivity;
import com.mingtu.center.bean.StatisticsBean;
import com.mingtu.common.base.BaseFragment;
import com.mingtu.common.callback.StringDialogCallback;
import com.hjq.gson.factory.GsonFactory;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.IntentUtils;
import com.mingtu.common.utils.MyLogUtil;
import com.mingtu.common.utils.MyUtills;
import com.mingtu.common.utils.SPTools;
import com.mingtu.common.view.MyAutofitTextView.AutofitTextView;
import com.mingtu.common.viewpager.LazyViewPager.LazyFragmentPagerAdapter;
import com.mingtu.guards.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.OnClick;

public class Tab23Page extends BaseFragment implements LazyFragmentPagerAdapter.Laziable {

    @BindView(R.id.layout_ranking)
    LinearLayout layoutRanking;
    @BindView(R.id.tv_patrol_history)
    TextView tvPatrolHistory;
    @BindView(R.id.aftv_name)
    AutofitTextView aftvName;
    @BindView(R.id.tv_trask_mileage)
    TextView tvTraskMileage;
    @BindView(R.id.tv_trask_duration)
    TextView tvTraskDuration;
    @BindView(R.id.tv_trask_times)
    TextView tvTraskTimes;
    @BindView(R.id.tv_event_num)
    TextView tvEventNum;
    @BindView(R.id.tv_event_pic)
    TextView tvEventPic;
    @BindView(R.id.tv_event_video)
    TextView tvEventVideo;
    @BindView(R.id.tv_ocr_num)
    TextView tvOcrNum;
    @BindView(R.id.tv_grade)
    TextView tvGrade;
    @BindView(R.id.layout_grade)
    LinearLayout layoutGrade;
    @BindView(R.id.iv_grade)
    ImageView ivGrade;
    @BindView(R.id.srl_up)
    SmartRefreshLayout srlUp;
    private int[] gradeImg = {R.mipmap.icon_grade1, R.mipmap.icon_grade2, R.mipmap.icon_grade3, R.mipmap.icon_grade4, R.mipmap.icon_grade5};
    private String gradeType;
    private String taskMileage;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_statistics;
    }

    @Override
    protected void initView(View view) {

    }

    @Override
    protected void initData() {
        String name = SPStaticUtils.getString(SPTools.userName);
        if (!StringUtils.isEmpty(name)) {
            if (name.length() == 1) {
                aftvName.setText(name);
            } else if (name.length() > 1) {
                aftvName.setText(name.substring(1, name.length()));
            }
        }
        initPullRefresh();//刷新
    }

    @Override
    protected void getData() {
        getStatisticsData();
    }


    @OnClick({R.id.layout_ranking, R.id.tv_patrol_history, R.id.layout_grade})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_ranking:
                IntentUtils.getInstance().readyGo(getActivity(), Ranking2Activity.class);
                break;
            case R.id.tv_patrol_history:
                IntentUtils.getInstance().readyGo(getActivity(), RecordActivity.class);
                break;
            case R.id.layout_grade:
                Bundle bundle = new Bundle();
                bundle.putString("gradeType", gradeType);
                bundle.putString("taskMileage", taskMileage);
                IntentUtils.getInstance().readyGo(getActivity(), TrackGradeActivity.class, bundle);
                break;
        }
    }


    private void initPullRefresh() {
        srlUp.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
        srlUp.setEnableLoadMore(false);
        srlUp.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                srlUp.finishRefresh(Constant.REFRESH_TIME);
                getStatisticsData();
            }
        });
    }


    /**
     * 获取统计数据
     */
    private void getStatisticsData() {
        OkGo.<String>post(Constant.getInstance().POST_STAT)
                .tag(this)
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            StatisticsBean bean = null;
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                bean = new StatisticsBean();
                                bean = gson.fromJson(result, StatisticsBean.class);

                                StatisticsBean.CurrentWeekBean currentWeekBean = bean.getCurrentWeek();

                                if (currentWeekBean != null) {

                                    // 巡护里程
                                    taskMileage = currentWeekBean.getTaskMileage();
                                    String taskDuration = currentWeekBean.getTaskDuration();// 巡护时长
                                    String taskNum = currentWeekBean.getTaskNum();// 巡护次数
                                    String eventNum = currentWeekBean.getEventNum();// 上报次数
                                    String picNum = currentWeekBean.getEventPic();// 上图片张数

                                    String videoNum = currentWeekBean.getEventVideo();// 视频个数
                                    String ocrNum = currentWeekBean.getOcrNum();// 识别个数
                                    //级别
                                    gradeType = currentWeekBean.getGradeType();

                                    if (!StringUtils.isEmpty(taskMileage)) {
                                        tvTraskMileage.setText(taskMileage + "km");
                                    }

                                    if (!StringUtils.isEmpty(taskDuration)) {

                                        long d = 0;
                                        if (taskDuration.indexOf(".") != -1) {
                                            String array[] = taskDuration.split("\\.");
                                            d = Long.parseLong(array[0]);
                                        } else {

                                            d = Long.parseLong(taskDuration);
                                        }
                                        tvTraskDuration.setText(MyUtills.formatSeconds(d));
                                    }
                                    if (!StringUtils.isEmpty(taskNum)) {
                                        tvTraskTimes.setText(taskNum + "次");
                                    }

                                    if (!StringUtils.isEmpty(eventNum)) {
                                        tvEventNum.setText(eventNum + "次");
                                    }
                                    if (!StringUtils.isEmpty(picNum)) {
                                        tvEventPic.setText(picNum + "张");
                                    }
                                    if (!StringUtils.isEmpty(videoNum)) {
                                        tvEventVideo.setText(videoNum + "个");
                                    }
                                    if (!StringUtils.isEmpty(ocrNum)) {
                                        tvOcrNum.setText(ocrNum+"个");
                                    }

                                    if (!StringUtils.isEmpty(gradeType)) {
                                        tvGrade.setText(gradeType);
                                    }

                                    if (gradeType.equals(context.getResources().getString(R.string.text_grade1))) {
                                        ivGrade.setImageResource(gradeImg[0]);
                                    }
                                    if (gradeType.equals(context.getResources().getString(R.string.text_grade2))) {
                                        ivGrade.setImageResource(gradeImg[1]);
                                    }
                                    if (gradeType.equals(context.getResources().getString(R.string.text_grade3))) {
                                        ivGrade.setImageResource(gradeImg[2]);
                                    }
                                    if (gradeType.equals(context.getResources().getString(R.string.text_grade4))) {
                                        ivGrade.setImageResource(gradeImg[3]);
                                    }
                                    if (gradeType.equals(context.getResources().getString(R.string.text_grade5))) {
                                        ivGrade.setImageResource(gradeImg[4]);
                                    }


                                }
                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                            }

                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        String result = response.body();
                        MyLogUtil.e(getClass().getSimpleName(), "onError==" + result);

                    }
                });
    }


}